package com.atguigu.util;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.EduConfig;
import redis.clients.jedis.Jedis;
import scala.Tuple2;

import java.sql.SQLException;
import java.util.List;

/**
 * @author hfh
 * @version 1.0
 * @date 2022/8/28 18:06
 */
public class DimUtil {
    public static JSONObject getDimInfo(DruidPooledConnection connection, String tableName, String id) {

        JSONObject result=null;
        List<JSONObject> dimInfos = getDimInfo(connection, tableName, new Tuple2<>("ID", id));
        if (dimInfos.size() > 0){
            result = dimInfos.get(0);
        }

        if (connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static List<JSONObject> getDimInfo(DruidPooledConnection connection, String tableName, Tuple2<String,String>... Tuple2s){
        //select * from table where ID = id and ...
        StringBuilder sql = new StringBuilder();
        sql.append("select * from ")
                .append(EduConfig.HBASE_SCHEMA)
                .append(".")
                .append(tableName)
                .append(" where ");
        for (int i = 0; i < Tuple2s.length; i++) {
            sql.append(Tuple2s[i]._1)
                    .append(" = '")
                    .append(Tuple2s[i]._2)
                    .append("'");
            if (i<Tuple2s.length-1){
                sql.append(" and ");
            }
        }

//        System.out.println(sql);

        List<JSONObject> jsonObjects = PhoenixUtil.sqlQuery(connection, sql.toString(), JSONObject.class);
        return jsonObjects;
    }
}
